package edu.iscas.appcheckclient.Adpater;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import edu.iscas.appcheckclient.BuildConfig;
import edu.iscas.appcheckclient.R;
import edu.iscas.appcheckclient.service.download.DownLoadService;

/**
 * Created by tangbo on 16/9/5.
 */
public class MyAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<Map<String, Object>> mData;
    public static Map<Integer, Boolean> isSelected;
    public Context context;

    public MyAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        this.context = context;
    }
    public void setMDate(JSONArray jsonArray)
    {
        List<Map<String, Object>> mData = new ArrayList<Map<String, Object>>();
        for (int i=0;i<jsonArray.length();i++)
        {
            Map<String, Object> temp = new HashMap<String, Object>();
            try {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Iterator<String> it =  jsonObject.keys();
                while (it.hasNext())
                {
                    String key = it.next();
                    temp.put(key, jsonObject.get(key));
                    System.out.print(key+":"+ jsonObject.get(key).toString());
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            mData.add(temp);
        }

        this.mData = mData;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        //convertView为null的时候初始化convertView。
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.list, null);
            holder.apkName = (TextView) convertView.findViewById(R.id.apk_name);
            holder.download = (Button) convertView.findViewById(R.id.download);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.apkName.setText(mData.get(position).get("filename").toString());
        holder.download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,mData.get(position).get("path").toString()+mData.get(position).get("filename").toString(),Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(context,DownLoadService.class);
                intent.putExtra("download_url","http://" + BuildConfig.IP + ":" + BuildConfig.PORT+context.getResources().getString(R.string.upload_apk)
                +"/"+mData.get(position).get("_id").toString()+"/apkfile");
                intent.putExtra("fileName", mData.get(position).get("filename").toString());
                context.startService(intent);
            }
        });
        return convertView;
    }

    public final class ViewHolder {
        public TextView apkName;
        public Button download;
    }
}
